<?php
class Homepage_model extends MY_Model
{
	function __construct()
	{
		parent::__construct();
	}
	
	function admin($id = 0)
	{
		// Logged User Data
		$sql = "SELECT * from prc_dashboard(".$id.")";  
		$this->data['user'] = $this->db->query($sql)->row();

		// Actions of Logged User 
		$sql = "SELECT message,date_created 
				FROM watchdog 
				WHERE id_admins = ".$id."              
				ORDER BY date_created DESC
				LIMIT 10";		
		$this->data['actions'] = $this->db->query($sql)->result();
	}	
	
	function user($id = 0)
	{
		// Logged User Data
		$sql = "SELECT * from prc_dashboard(".$id.")";  
		$this->data['user'] = $this->db->query($sql)->row();

		// Actions of Logged User 
		$sql = "select distinct 
				max(ad.name) as name,
				max(ad.file) as file,
				max(ad.duration) as duration, 
				max(ca.cost)* cast ((select value from settings.settings where name='comp_coef') as numeric(10,2)) as cost, 
				max(ads.name) as advertiser,count(cl.id)				
				from stats.calls_all cl
				join campaigns_adverts ca on ca.id = cl.id_campaigns_adverts
				join adverts ad on ad.id = ca.id_adverts
				join advertisers ads on ads.id = ad.id_advertisers
				where ad.status = 1
				group by ad.id
				
				order by count(cl.id) desc
				limit 10";		
		$this->data['adverts'] = $this->db->query($sql)->result();
		
		// Balance User Data
		$sql = "select	coalesce(ua.num_adv_calls,0) as num_adv_calls, 
						coalesce(ua.bonus_debit, 0)+  
						coalesce((select sum(amount)
						from stats.users_payments 
						where id_users = ".$id."
						and type = 0
						),0)
						as bonus_debit, 
						coalesce(ua.bonus_credit,0) as bonus_credit, 
						coalesce(u.balance,0) as balance
				from public.users u
				left join stats.users_all ua on u.id = ua.id_users
				where u.id = ".$id;  
		$balance = $this->db->query($sql)->row();
		
		return $balance; 
	}	

	
}
?>